package model;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.InputSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.CharacterData;
import org.xml.sax.SAXException;


public class ParseXML {

  public ParseXML(){
	  
  }
  // Node is on the second level under a parent node pNodeName
  public ArrayList<String> getCharValueForANode(String xmlStr, String NodeName1, String nodeName2) throws ParserConfigurationException, SAXException, IOException{
	  	ArrayList<String> nodeValues = new ArrayList<String>();

	    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	    InputSource is = new InputSource();
	    is.setCharacterStream(new StringReader(xmlStr));

	    Document doc = docBuilder.parse(is);
	    NodeList node1 = doc.getElementsByTagName(NodeName1);
	    NodeList node2 = doc.getElementsByTagName(nodeName2);
	    //System.out.println(node1.getLength());
	    for (int i = 0; i < node1.getLength(); i++) {
	    	Element element = (Element)node1.item(i);
	    	nodeValues.add(getValueForElement(element));
	    	//System.out.println(getValueForElement(element));
	    	Element element1 = (Element)node2.item(i);
	    	nodeValues.add(getValueForElement(element1));
	    	//System.out.println(getValueForElement(element1));
	    }
	    return nodeValues;
  }  

  public ArrayList<String> getCharValueForANode(String xmlStr, String NodeName1, String nodeName2,String nodeName3,String nodeName4, String nodeName5) throws ParserConfigurationException, SAXException, IOException{
	  	ArrayList<String> nodeValues = new ArrayList<String>();

	    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
	    InputSource is = new InputSource();
	    is.setCharacterStream(new StringReader(xmlStr));

	    Document doc = docBuilder.parse(is);
	    NodeList node1 = doc.getElementsByTagName(NodeName1);
	    NodeList node2 = doc.getElementsByTagName(nodeName2);
	    NodeList node3 = doc.getElementsByTagName(nodeName3);
	    NodeList node4 = doc.getElementsByTagName(nodeName4);
	    NodeList node5 = doc.getElementsByTagName(nodeName5);
	    //System.out.println(node5.getLength());
    	Element element = (Element)node1.item(0);
    	nodeValues.add(getValueForElement(element));
    	//System.out.println(getValueForElement(element));
    	Element element1 = (Element)node2.item(0);
    	nodeValues.add(getValueForElement(element1));
    	Element element2 = (Element)node3.item(0);
    	nodeValues.add(getValueForElement(element2));
    	//System.out.println(getValueForElement(element2));
    	Element element3 = (Element)node4.item(0);
    	nodeValues.add(getValueForElement(element3));
    	//System.out.println(getValueForElement(element3));
    	
    	for(int i = 0 ; i < node5.getLength() ; i++){
    		Element element4 = (Element)node5.item(0);
        	nodeValues.add(getValueForElement(element4));
        	//System.out.println(getValueForElement(element4));
    	}
	    return nodeValues;
}  
  // Node is on the first level under root node 
  public ArrayList<String> getCharValueForANode(String xmlStr, String nodeName) throws ParserConfigurationException, SAXException, IOException{
    ArrayList<String> nodeValues = new ArrayList<String>();

    DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(xmlStr));

    Document doc = docBuilder.parse(is);
    NodeList nodes = doc.getElementsByTagName(nodeName);

    if ( nodes != null ) {
        for(int i = 0 ; i < nodes.getLength() ; i++){
            Element element = (Element)nodes.item(i);
            nodeValues.add(getValueForElement(element));
        }
    }
    return nodeValues;
  }

  private String getValueForElement(Element ele) {
    Node firstChild = ele.getFirstChild();
    String value = "";
    if (firstChild instanceof CharacterData) {
      CharacterData charData = (CharacterData)firstChild;
      value = charData.getData();
    }
    return value;
  }
}

